package vip.web3.admin.sys.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;
import vip.web3.admin.sys.entity.biz.AOrderEntity;
import vip.web3.admin.sys.vo.biz.OrderVo;
import vip.web3.admin.sys.vo.biz.export.ExOrderVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface AOrderMapper {

    AOrderMapper INSTANCE = Mappers.getMapper(AOrderMapper.class);

    @Mapping(target = "priceCny", ignore = true)
    @Mapping(target = "priceUsdt", ignore = true)
    @Mapping(target = "quantity", ignore = true)
    @Mapping(target = "amountCny", ignore = true)
    @Mapping(target = "amountUsdt", ignore = true)
    @Mapping(target = "rate", ignore = true)
    @Mapping(target = "income", ignore = true)
    @Mapping(target = "totalIncome", ignore = true)
    @Mapping(target = "leftIncome", ignore = true)
    OrderVo toOrderVo(AOrderEntity entity);

    default OrderVo toAdminOrderVo(AOrderEntity entity) {
        OrderVo orderVo = toOrderVo(entity);
        if (Objects.nonNull(orderVo) && Objects.nonNull(entity)) {
            BigDecimal priceCny = Objects.isNull(entity.getPriceCny()) ? BigDecimal.ZERO : entity.getPriceCny();
            BigDecimal priceUsdt = Objects.isNull(entity.getPriceUsdt()) ? BigDecimal.ZERO : entity.getPriceUsdt();
            BigDecimal quantity = Objects.isNull(entity.getQuantity()) ? BigDecimal.ZERO : entity.getQuantity();
            BigDecimal amountCny = Objects.isNull(entity.getAmountCny()) ? BigDecimal.ZERO : entity.getAmountCny();
            BigDecimal amountUsdt = Objects.isNull(entity.getAmountUsdt()) ? BigDecimal.ZERO : entity.getAmountUsdt();
            BigDecimal rate = Objects.isNull(entity.getRate()) ? BigDecimal.ZERO : entity.getRate();
            BigDecimal income = Objects.isNull(entity.getIncome()) ? BigDecimal.ZERO : entity.getIncome();
            BigDecimal totalIncome = Objects.isNull(entity.getTotalIncome()) ? BigDecimal.ZERO : entity.getTotalIncome();
            BigDecimal leftIncome = Objects.isNull(entity.getLeftIncome()) ? BigDecimal.ZERO : entity.getLeftIncome();

            orderVo.setPriceCny(priceCny.stripTrailingZeros().toPlainString());
            orderVo.setPriceUsdt(priceUsdt.stripTrailingZeros().toPlainString());
            orderVo.setQuantity(quantity.stripTrailingZeros().toPlainString());
            orderVo.setAmountCny(amountCny.stripTrailingZeros().toPlainString());
            orderVo.setAmountUsdt(amountUsdt.stripTrailingZeros().toPlainString());
            orderVo.setRate(rate.stripTrailingZeros().toPlainString());
            orderVo.setIncome(income.stripTrailingZeros().toPlainString());
            orderVo.setTotalIncome(totalIncome.stripTrailingZeros().toPlainString());
            orderVo.setLeftIncome(leftIncome.stripTrailingZeros().toPlainString());
        }
        return orderVo;
    }

    default List<OrderVo> toAdminOrderVoList(List<AOrderEntity> entity) {
        if (CollectionUtils.isEmpty(entity)) {
            return null;
        }

        List<OrderVo> list = new ArrayList<>(entity.size());
        for (AOrderEntity recordEntity : entity) {
            list.add(this.toAdminOrderVo(recordEntity));
        }

        return list;
    }

    List<ExOrderVo> toExOrderVoList(List<OrderVo> adminOrderVoList);
}
